package com.yzkj.siot.service.impl;

import com.yzkj.siot.context.CurUserCtx;
import com.yzkj.siot.domain.TenantDevice;
import com.yzkj.siot.domain.TenantDeviceData;
import com.yzkj.siot.domain.TenantProduct;
import com.yzkj.siot.domain.TenantProductData;
import com.yzkj.siot.dto.DeviceDataDTO;
import com.yzkj.siot.facade.DeviceFacade;
import com.yzkj.siot.facade.ProductDataFacade;
import com.yzkj.siot.facade.ProductFacade;
import com.yzkj.siot.service.*;
import com.yzkj.siot.uesr.dto.CurrentUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DeviceDataHandlerImpl implements DeviceDataHandler {

    @Autowired
    TenantDeviceDataService deviceDataService;

    @Autowired
    ProductDataFacade productDataFacade;

    @Autowired
    ProductFacade productFacade;

    @Autowired
    DeviceFacade deviceFacade;

    @Autowired
    TenantProductDataService productDataService;

    @Autowired
    TenantProductService productService;

    @Autowired
    TenantDeviceService deviceService;

    @Override
    public void dataReceived(DeviceDataDTO deviceDataDTO) {
        String data = deviceDataDTO.getRawData();
        String deviceIp = deviceDataDTO.getDeviceIp();

        CurrentUser currentUser = new CurrentUser();
        currentUser.setTenantId(deviceDataDTO.getTenantId());
        CurUserCtx.set(currentUser);

        // 根据设备ip获取设备信息
        TenantDevice device = deviceFacade.queryOneByIp(deviceDataDTO.getTenantId(), deviceIp);

        if (device == null) {
            log.warn("设备Ip不存在");
            return;
        }

        TenantProduct product = device.getProduct();
        List<TenantProductData> productDataList = product.getProductDataList();

        try {
            List<TenantDeviceData> insList = new ArrayList<>();
            for (TenantProductData item : productDataList) {
                String itemData = data.substring(item.getStartPos(), item.getEndPos() + 1);
                Integer dataContent = null;

                if (item.getIsBoolean()) {
                    if (item.getBooleanTrue().equals(itemData)) {
                        dataContent = 1;
                    } else if(item.getBooleanFalse().equals(itemData)) {
                        dataContent = 0;
                    }
                } else {
                    dataContent = Integer.valueOf(itemData, product.getDataBase());
                }

                TenantDeviceData insData = new TenantDeviceData();
                insData.setTenantId(currentUser.getTenantId());
                insData.setDeviceId(device.getId());
                insData.setProductDataId(item.getId());
                insData.setDataContent(dataContent);
                insData.setRawData(itemData);
                insData.setIsBoolean(item.getIsBoolean());

                insList.add(insData);
            }
            deviceDataService.saveBatch(insList);
        } catch (Exception e) {
            log.error("数据解析失败", e);
        }
    }
}
